<?php
namespace slinks\extensions\amf\zend;

use slinks\core\DependencyInjection\ContainerInterface;

use slinks\core\DependencyInjection\ContainerAwareInterface;
/**
 * Adds support for RemoteMethod annotations to the Zend_Amf_Server.
 * 
 * @author Navid Mitchell
 * @Controller
 */
class SlinksAmfController implements ContainerAwareInterface{ 
	
	/**
	 * @var SlinksAmfServer $slinksAmfServer the amf server that all calls are proxied to. 
	 */
	private $slinksAmfServer;
	
	private static $zendPathSet=null;
	
	private function __construct($pathToZendDir,$proxyDoctrineCollections){
		
		if(!isset(self::$zendPathSet)){
			// Setup include path
			//add zend directory to include path
			set_include_path(get_include_path().PATH_SEPARATOR.$pathToZendDir);
			
			// Initialize Zend Framework loader
			require_once 'Zend/Loader/Autoloader.php';
			\Zend_Loader_Autoloader::getInstance();
			
			self::$zendPathSet = true;
		}
		
		$this->slinksAmfServer = new SlinksAmfServer($proxyDoctrineCollections);
	}
	
	
	/**
	 * Creates a new SlinksAmfServer and adds Zend to the include path. 
	 * @param string $pathToZendDir the path to the Zend Amf framework classes. This should be the root zend directory. 
	 * @FactoryMethod('%zendDir%')
	 */
	public static function create($pathToZendDir,$proxyDoctrineCollections){
		return new SlinksAmfController($pathToZendDir,$proxyDoctrineCollections);
	}
	
	/**
     * Sets the Container.
     *
     * @param ContainerInterface $container A ContainerInterface instance
     */
	public function setContainer(ContainerInterface $container = null){
		$this->slinksAmfServer->setContainer($container);
	}
	
	
    /**
     * Proxy calls to SlinksAmfServer. 
     *
     * @param string $method
     * @param array $args
     * @return mixed
     */
    public function __call($method, $args){
        if (method_exists($this->slinksAmfServer, $method)) {
            return call_user_func_array(array($this->slinksAmfServer, $method), $args);
        }else{
        	throw new \BadMethodCallException('Method '.$method.' not implemented for SlinksAmfServer.');
        }
    }
	


}

